package cn.cdqf.web;

import cn.cdqf.bean.Statistics;
import cn.cdqf.bean.TDepart;
import cn.cdqf.common.ResultResponse;
import cn.cdqf.service.DepartService;
import cn.cdqf.service.EmpService;
import cn.cdqf.utils.PageUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class DepartServlet {
    @Autowired
    private DepartService departService;

    @Autowired
    private EmpService empService;

    @RequestMapping("/queryDepart/{pageIndex}/{pageSize}")
    @ResponseBody
    public PageUtils queryDepart(@PathVariable("pageIndex") int pageIndex,@PathVariable("pageSize") int pageSize){
        System.out.println(pageIndex+"+"+pageSize);
        int totalCount = departService.getTotalCount();
        int pageStart = (pageIndex-1)*pageSize;
        List<TDepart> tDeparts = departService.queryDepart(pageStart,pageSize);
        PageUtils pageUtils = new PageUtils(pageIndex,pageSize,totalCount,tDeparts);
        return pageUtils;
    }

    @RequestMapping("/deleteDepart")
    @ResponseBody
    public ResultResponse  deleteDepart(String id){
        int i = departService.deleteDepart(Integer.parseInt(id));
        ResultResponse resultResponse =null;
        if (i>0){
            resultResponse = new ResultResponse(100,"删除成功");
        }else {
            resultResponse = new ResultResponse(200,"删除失败");
        }
        return resultResponse;
    }

    @ResponseBody
    @RequestMapping("/addDepart")
    public ResultResponse addDepart(String name ,String createtime){
        TDepart tDepart = new TDepart();
        tDepart.setName(name);
        tDepart.setCreatetime(createtime);
        int i = departService.addDepart(tDepart);
        ResultResponse resultResponse = null;
        if (i> 0){
            resultResponse = new ResultResponse(100,"添加成功");
        }else {
            resultResponse = new ResultResponse(200,"添加失败");
        }
        return resultResponse;
    }

    @ResponseBody
    @RequestMapping("/queryDepartById")
    public String queryDepartById(String id){
        TDepart tDepart = departService.queryDepartById(Integer.parseInt(id));
        return JSON.toJSONString(tDepart);
    }



    @ResponseBody
    @RequestMapping("/updateDepart")
    public ResultResponse updateDepart(String id,String name,String createtime){
        int i = departService.updateDepart(Integer.parseInt(id),name,createtime);
        ResultResponse resultResponse = null;
        if (i> 0){
            resultResponse = new ResultResponse(100,"更新成功");
        }else {
            resultResponse = new ResultResponse(200,"更新失败");
        }
        return resultResponse;
    }

    @ResponseBody
    @RequestMapping("/getDeparts")
    public List<TDepart> getDeparts() {
        return departService.getDeparts();
    }

    @ResponseBody
    @RequestMapping("/getDepartNameJson")
    public String getDepartNameJson(){
        List<TDepart> departs = departService.getDeparts();
        List<String> dname = new ArrayList<>();
        for (TDepart depart : departs) {
            dname.add(depart.getName());
        }
        System.out.println(JSONObject.toJSONString(dname));
        String a = JSONObject.toJSONString(dname);

        List<Statistics> statistics = empService.getStatistics();
        System.out.println(JSONObject.toJSONString(statistics));
        String b = JSONObject.toJSONString(statistics);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("a",a);
        jsonObject.put("b",b);

        return JSON.toJSONString(jsonObject);
    }
}
